﻿DELIMITER //

--
--
DROP PROCEDURE IF EXISTS spZeusUserLogin 
//

-- HU - Bejelentkezés
-- EN - Authentication
--
CREATE PROCEDURE spZeusUserLogin 
  (_Code VARCHAR(20), _Password VARCHAR(20))
BEGIN
  DECLARE _Rows INTEGER;

  DECLARE _ID INTEGER;
  DECLARE _Pswd1 NVARCHAR(40);
  DECLARE _Pswd2 NVARCHAR(40);

  DECLARE _Lang NVARCHAR(10);

  -- Nyelvi beállítás
  --
  SELECT  
      CASE @@lc_time_names
        WHEN N'hu_HU' THEN N'HU'
        WHEN N'en_US' THEN N'EN'
        WHEN N'en_UK' THEN N'EN'
        WHEN N'de_DE' THEN N'DE'
        WHEN N'de_AT' THEN N'DE'
      ELSE N'EN' END INTO _Lang;


  -- Check user
  --
  BEGIN
    DECLARE EXIT HANDLER FOR NOT FOUND SET _Rows = 0;

    SELECT ID, Password
      INTO _ID, _Pswd1
      FROM ZeusUser
     WHERE Code = _Code;

    SET _Rows = 1;
  END;

  IF (_Rows != 1) THEN

    IF (_Lang = 'HU') THEN
      SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = N'Bejelentkezés elutasítva !';
    END IF;

    IF (_Lang = 'EN') THEN
      SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = N'Access denied !';
    END IF;

    IF (_Lang = 'DE') THEN
      SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = N'Einloggen Abgelehnt!';
    END IF;
  END IF;

  -- Titkosan hasonlítja össze
  --
  SET _Pswd2 = MD5(_Password);

  IF (_Pswd1 != _Pswd2) THEN

    IF (_Lang = 'HU') THEN
      SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = N'Bejelentkezés elutasítva !';
    END IF;

    IF (_Lang = 'EN') THEN
      SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = N'Access denied !';
    END IF;

    IF (_Lang = 'DE') THEN
      SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = N'Einloggen Abgelehnt!';
    END IF;
  END IF;


  -- Eredményhalmaz
  --
  SELECT ID, Name
    FROM ZeusUser
   WHERE ID = _ID;
END
//
